package com.basic.guava;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class GuavaDemo {

	@Test
	public void optional(){
		Optional<Integer> optional = Optional.of(1);
		System.out.println(optional.isPresent());
		System.out.println(optional.get());
		Optional<Integer> optional2 = Optional.fromNullable(2);
		System.out.println(optional2.or(3));
	}
	
	@Test
	public void beforCheck(){
//		Preconditions.checkArgument(1>0, "参数异常：%s",1);
//		Preconditions.checkNotNull(1, "null 检查！");
//		Preconditions.checkElementIndex(3, 1, "元素索引检查！");
//		Preconditions.checkPositionIndex(2, 1, "元素位置检查！");
		Preconditions.checkPositionIndexes(1, 2, 3);
	}
	
	@Test
	public void ObjectTest(){
		System.out.println(Objects.equal("1", null));
		System.out.println(Objects.equal(Objects.hashCode("1","2","4"), Objects.hashCode("1","2","3")));
	}
	
	@Test
	public void ordering(){
		ArrayList<Integer> list = Lists.newArrayList(3,4,7,10);
		Ordering<Integer> ordering = Ordering.natural();
//		List<Integer> list2 = ordering.greatestOf(list, 1);
//		list2.forEach(i ->  System.out.println(i));
//		System.out.println(ordering.max(list));
		System.out.println(ordering.isOrdered(list));
	}
	
	@Test
	public void collection(){
		ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3, 4, 5);
		ImmutableList<Integer> list = immutableList.asList();
		list.forEach(i ->  System.out.println(i));
		System.out.println("------------------");
		ImmutableList<Integer> copyOfList = ImmutableList.copyOf(list);
		ImmutableList<Integer> asList = copyOfList.asList();
		asList.forEach(i ->  System.out.println(i));
	}
	
	@Test
	public void hashMultiset(){
		HashMultiset<Integer> hashMultiset = HashMultiset.create();
//		hashMultiset.add(1);
		hashMultiset.add(3, 1);
		hashMultiset.add(2, 2);
		hashMultiset.remove(2);
		System.out.println(hashMultiset.count(1));
		Set<Integer> elementSet = hashMultiset.elementSet();
		elementSet.forEach(i ->  System.out.println(i));
		Set<Multiset.Entry<Integer>> entrySet = hashMultiset.entrySet();
		entrySet.forEach(i ->  System.out.println(i.getElement()+" : "+i.getCount()));
		
	}
	
	@Test
	public void multimap(){
		HashMultimap<String,String> objHashMultimap = HashMultimap.create();
		objHashMultimap.put("test", "test");
		objHashMultimap.put("test", "test2");
		objHashMultimap.put("test", "test3");
//		Map<String, Collection<String>> asMap = objHashMultimap.asMap();
//		asMap.get("test").forEach(i -> System.out.println(i));
		System.out.println("------------keyset-------------");
		Collection<String> keySet = objHashMultimap.keySet();
		keySet.forEach(i -> System.out.println(i));
		System.out.println("------------keys-------------");
		Collection<String> keys = objHashMultimap.keys();
		keys.forEach(i -> System.out.println(i));
		System.out.println("------------values-------------");
		Collection<String> values = objHashMultimap.values();
		values.forEach(i -> System.out.println(i));
	}
	
	@Test
	public void collectionUtil(){
		ArrayList<Integer> arrayList = Lists.newArrayList(1,2,3,4,2,3,5);
		ArrayList<Integer> arrayList2 = Lists.newArrayList(1,2,3);
		Iterable<Integer> concat = Iterables.concat(arrayList,arrayList2);
//		concat.forEach(i -> System.out.println(i));
//		System.out.println(Iterables.frequency(concat, 2));;
//		Iterable<List<Integer>> partition = Iterables.partition(concat, 5);
//		partition.forEach(i -> System.out.println(i));
//		System.out.println(Iterables.getOnlyElement(arrayList2));;
		
		HashSet<Integer> newHashSet1 = Sets.newHashSet(1,2,3,4,5);
		HashSet<Integer> newHashSet2 = Sets.newHashSet(1,2,3,4);
//		SetView<Integer> intersection = Sets.intersection(newHashSet1, newHashSet2);
//		intersection.forEach(i -> System.out.println(i));
		System.out.println("------------");
		SetView<Integer> difference = Sets.difference(newHashSet1, newHashSet2);
		difference.forEach(i -> System.out.println(i));
	}
}
